
$(function() {
	console.log("base="+base);
    var jqXHR = $('#promo_fileupload').fileupload({
      url: base+"/bankCardBind/uploadExcel",
      dataType: 'json',
      start: function (e) {
        console.log("promo_fileupload start ");
        $("#progress-area").removeClass("am-hide");
        $("#progress-text").removeClass("am-text-danger");
        $("#progress-text").html("");
        $(".am-progress-bar").css("width","0%");
      },
      done: function (e, data) {
        if(data.result.status=="error"){
        	showImageInvalidAlert("导入失败："+data.result.message,'error');
        	setTimeout(function() {
        		$("#progress-area").addClass("am-hide");
        	},1500);
        	return false;
        }else{

          showErrorLogs(data.result.errorLogs);
          showImageInvalidAlert(data.result.message,'success');
          setTimeout(function() {
            $("#progress-area").addClass("am-hide");
          },1500);
          clearImageValidInfo("");
        }
      },
      progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $(".am-progress-bar").css("width",progress + "%");
        $("#progress-text").html(progress + "%");
      },
      error: function (jqXHR, textStatus, errorThrown) {
        $("#progress-area").removeClass("am-hide");
        $("#progress-text").addClass("am-text-danger");
        $("#progress-text").html("导入数据失败，系统异常！");
        $(".am-progress-bar").css("width","0%");
        setTimeout(function() {
          $("#progress-area").addClass("am-hide");
        },2000);
      },
      fail: function (jqXHR, textStatus) {
        $("#progress-area").removeClass("am-hide");
        $("#progress-text").addClass("am-text-danger");
        $("#progress-text").html("导入数据失败，系统异常！");
        $(".am-progress-bar").css("width","0%");
        setTimeout(function() {
          $("#progress-area").addClass("am-hide");    
        },2000);
      }
    });

});

function showImageInvalidAlert(message,type){
  var $alert = $("#image_valid").find('.am-alert');
  if (!$alert.length) {
    $alert = $('<div class="am-alert am-alert-danger"></div>').hide()
    $alert.appendTo($("#image_valid"));
  }
 if(type=='error'){
   $("#image_valid").find('.am-alert').addClass("am-alert-danger");
   $("#image_valid").find('.am-alert').removeClass("am-alert-success");
 }else{
   $("#image_valid").find('.am-alert').addClass("am-alert-success");
   $("#image_valid").find('.am-alert').removeClass("am-alert-danger");
 }


  if(message.length==0){
    $alert.hide();
  }else{
    $alert.text(message).show();
    setTimeout(function() {
      $alert.text(message).show();
    },50);
  }
}

function clearImageValidInfo(){
  showImageInvalidAlert("");
}
function goBackList (){
  window.location.href = base+'/bankCardBind/list';
}
function showErrorLogs(errorLogs){
  if(errorLogs != null && errorLogs.length > 0){
    //$("#error_div").style().display=='none';
    document.getElementById("errorLogs").innerHTML = "";
    document.getElementById('error_div').style.display='block';
    for(var i=0;i<errorLogs.length;i++){
      var errorLog = errorLogs[i];
      var $alert =  $('<div class="am-alert am-alert-danger">'+errorLog+'</div>');
      $alert.appendTo($("#errorLogs"));
      console.log(errorLog);
    }
  }else{
    document.getElementById('error_div').style.display='none';
  }
}